//
//  HPWattingView.m
//  HPZZTong
//
//  Created by handpay on 14-10-15.
//  Copyright (c) 2014年 handpay. All rights reserved.
//

#import "HPWaittingView.h"
#import "DefineUtil.h"
#import "ZZTongDefine.h"

@interface HPWaittingView ()

@property(nonatomic, copy) NSString* msgTitle;
@property(nonatomic) UIActivityIndicatorView* indicatorView;

@end

@implementation HPWaittingView

- (id)initWithMsg:(NSString*)msg
{
    CGRect frame = [[UIScreen mainScreen] bounds];
    self = [super initWithFrame:frame];
    if (self) {
        self.msgTitle = msg;
        [self initializeWithFrame:frame];
    }
    return self;
}

- (void)awakeFromNib{
    [self initializeWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
}

#pragma mark - 私有方法

- (void) initializeWithFrame:(CGRect)frame{
    
    self.backgroundColor = UIColorFromRGBA(0x000000, 0.60);
    
    self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    CGRect tempRect = self.indicatorView.frame;
    
    int viewW = 250;
    int lSpace = 25;
    int tSpace = 10;
    int viewH = 50;
    int w = viewW - tempRect.size.width - lSpace;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(lSpace + 15 + tempRect.size.width, tSpace,w, 1000.0f)];
    label.text = self.msgTitle;
    label.font = [UIFont systemFontOfSize:15.];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor blackColor];
    label.numberOfLines = 0;
    [label sizeToFit];
    
    CGRect labelFrame = label.frame;
    int h = viewH - 2 * tSpace;
    if (labelFrame.size.height < h) {
        labelFrame.size.height = h;
        label.frame = labelFrame;
    }else{
        viewH = labelFrame.size.height + 2 * tSpace;
    }
    
    if (labelFrame.size.width < w) {
        viewW = 3 * lSpace + tempRect.size.height + labelFrame.size.width;
    }
    
    tempRect.origin.x = lSpace;
    tempRect.origin.y = (viewH - tempRect.size.height) / 2;
    self.indicatorView.frame = tempRect;
    
    UIView *messageBoxView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, viewW, viewH)];
    messageBoxView.backgroundColor = [UIColor whiteColor];
    messageBoxView.layer.cornerRadius = 10;
    messageBoxView.center = CGPointMake(self.center.x, self.center.y);
    
    [messageBoxView addSubview:self.indicatorView];
    [messageBoxView addSubview:label];
    
    [self addSubview:messageBoxView];
    
}


- (void)showView{
    [[[UIApplication sharedApplication]keyWindow]addSubview:self];
    [self.indicatorView startAnimating];
}
- (void)removeView{
    [self removeFromSuperview];
}
@end
